package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Autowired
    private EduVideoService eduVideoService;

    //1 根据课程id删除小节
    //  删除小节，删除对应视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapper);
        ArrayList<String> videoList = new ArrayList<>();
        if (eduVideoList.size()>0){
            for (EduVideo eduVideo : eduVideoList) {
                String videoSourceId = eduVideo.getVideoSourceId();
                videoList.add(videoSourceId);
            }
            //批量删除视频
            vodClient.deleteBatch(videoList);
        }
        baseMapper.delete(wrapper);
    }
}
